package com.qtenv.datasync.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qtenv.datasync.service.entity.Zone;
import com.qtenv.datasync.service.mapper.ZoneMapper;
import com.qtenv.datasync.service.model.ZoneDataModel;
import com.qtenv.datasync.service.service.ZoneService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 层级表 服务实现类
 * </p>
 *
 * @author CBL
 * @since 2022-08-02
 */
@Service
public class ZoneServiceImpl extends ServiceImpl<ZoneMapper, Zone> implements ZoneService {
    @Override
    public List<Zone> getZones(String tenantId, String levelType) {
        List<Zone> zones = this.list(new QueryWrapper<Zone>().eq("tenant_id", tenantId)
                .in("LevelType", levelType.split(",")));

        List<ZoneDataModel> zoneDataModels = this.getBaseMapper().getZoneCxclList(tenantId);

        List<Zone> result = CollectionUtil.newArrayList();

        zones.forEach(zone -> {
            ZoneDataModel zdm = zoneDataModels.stream().filter(item -> item.getAreaID().equals(zone.getAreaID())).findFirst().orElse(null);
            zone.setZbx(zdm);
            result.add(zone);
        });

        return result;
    }

    @Override
    public List<Zone> getChildDmaList(String tenantId, String areaId) {
        return this.list(new QueryWrapper<Zone>().eq("tenant_id", tenantId).eq("PID", areaId).eq("LevelType", "2"));
    }

    @Override
    public List<Zone> getChildSeniorList(String tenantId, String areaId) {
        return this.list(new QueryWrapper<Zone>().eq("tenant_id", tenantId).eq("PID", areaId).eq("LevelType", "3"));
    }

    @Override
    public Page<Zone> getAllSeniorDataList(String tenantId, String areaName, String page, String pageSize) {
        QueryWrapper<Zone> query = new QueryWrapper<Zone>().eq("tenant_id", tenantId).eq("LevelType", "3");

        if (StrUtil.isNotEmpty(areaName)) {
            query = query.like("AreaName", areaName);
        }

        return this.getBaseMapper().selectPage(new Page(Integer.valueOf(page), Integer.valueOf(pageSize)), query);
    }
}
